Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
babel-traverse
Advanced tools
The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes
The babel-traverse package is part of the Babel compiler ecosystem and provides the ability to traverse and manipulate the abstract syntax tree (AST) generated from JavaScript code. It allows developers to analyze and rewrite code programmatically, which is useful for building compilers, code transformation tools, linters, and more.
Visiting Nodes
This feature allows you to visit and manipulate nodes in the AST. In the provided code, we change all identifiers named 'n' to 'x' in a simple function.
const babel = require('@babel/core');
const traverse = require('babel-traverse').default;
const code = `function square(n) { return n * n; }`;
const ast = babel.parse(code);
traverse(ast, {
enter(path) {
if (path.isIdentifier({ name: 'n' })) {
path.node.name = 'x';
}
}
});
console.log(babel.generate(ast).code);
Scope Manipulation
This feature involves manipulating the scope of variables. In the example, within a function declaration, the variable 'b' is renamed to 'c'.
const babel = require('@babel/core');
const traverse = require('babel-traverse').default;
const code = `let a = 2; function test() { let b = 4; }`;
const ast = babel.parse(code);
traverse(ast, {
FunctionDeclaration(path) {
path.scope.rename('b', 'c');
}
});
console.log(babel.generate(ast).code);
Recast is a JavaScript AST manipulation library that allows you to parse, modify, and print your code while preserving the original formatting as much as possible. It differs from babel-traverse in that it focuses more on preserving the code's original format and less on providing a comprehensive transformation toolkit.
Esprima is a high performance, standard-compliant ECMAScript parser that also allows you to traverse and analyze JavaScript code. Unlike babel-traverse, Esprima does not focus on transformations but rather on parsing and providing a detailed AST.
babel-traverse maintains the overall tree state, and is responsible for replacing, removing, and adding nodes.
$ npm install --save babel-traverse
We can use it alongside Babylon to traverse and update nodes:
import * as babylon from "babylon";
import traverse from "babel-traverse";
const code = `function square(n) {
return n * n;
}`;
const ast = babylon.parse(code);
traverse(ast, {
enter(path) {
if (path.isIdentifier({ name: "n" })) {
path.node.name = "x";
}
}
});
FAQs
The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes
The npm package babel-traverse receives a total of 1,793,700 weekly downloads. As such, babel-traverse popularity was classified as popular.
We found that babel-traverse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.